﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BTKERP.Model;
using BTKERP.BLL.Inventory;
using System.Web.Mvc;
using BTKERP.BLL.UserMgmt;
using BTKERP.Model.Web;

namespace BTKERP.MVCWeb.AppCode
{
    public class DataRefHelper
    {
        private static List<Unit> Units = null;
        private static object _lock = new object();
        public static string GetUnitTypeById(Guid id)
        {
            UnitType type = InventoryInfoMgt.GetUnitTypeById(id);
            if (type == null) return string.Empty;
            return type.Name;
        }

        public static string GetUserName(string id)
        {
            User  user = UserBLL.GetALlUsers().Find(n => n.UserID.ToString() == id);
            if (user == null) return string.Empty;
            return user.UserName;
        }

        public static List<Unit> GetUnits()
        {
            if (Units == null)
            {
                Units = InventoryInfoMgt.GetUnits();
            }
            return Units;
        }

        public static string GetUnitName(string id)
        {
            Unit unit = GetUnits().Find(n => n.Id.ToString() == id);
            if (unit != null) return unit.Name;
            return string.Empty;
        }

        public static void UpdateUnits()
        {
            lock (_lock)
            {
                Units = InventoryInfoMgt.GetUnits();
            }
        }

        public static int GetCurrentStock(Guid productId,Guid warehouseId)
        {
            WarehouseStock stock = InventoryInfoMgt.GetWarehouseStock(warehouseId, productId);
            if (stock != null) return (int)stock.AvailableQty;
            return 0; 
        }

        public static string GetUnitById(Guid id)
        {
            Unit unit = InventoryInfoMgt.GetUnitById(id);
            if (unit == null) return string.Empty;
            return unit.Name;
        }

        public static string GetCategoryById(Guid id)
        {
            Category category = InventoryInfoMgt.GetCategoryById(id);
            if (category == null) return string.Empty;
            return category.Name;
        }

        public static string GetSupplierById(Guid id)
        {
            Supplier supplier = InventoryInfoMgt.GetSupplierById(id);
            if (supplier == null) return string.Empty;
            return supplier.Name;
        }

        public static string GetCountryById(Guid id)
        {
            Country country = InventoryInfoMgt.GetCountryById(id);
            if (country == null) return string.Empty;
            return country.Name;
        }

        public static string GetCityById(Guid id)
        {
            City city = InventoryInfoMgt.GetCityById(id);
            if (city == null) return string.Empty;
            return city.Name;
        }

        public static string GetWarehouseById(Guid id)
        {
            Warehouse wh = InventoryInfoMgt.GetWarehouseById(id);
            if (wh == null) return string.Empty;
            return wh.Name;
        }


        public static string GetWarehouseByDOId(Guid id)
        {
            return InventoryInfoMgt.GetwarehouseByDOId(id);
        }

        public static string GetProductNameById(Guid id)
        {
            Product p = InventoryInfoMgt.GetProductById(id);
            if (p == null) return string.Empty;
            return p.Name;
        }

        public static List<SelectListItem> GetWareHouseList(bool includeAll = true, string id = "")
        {
            List<Warehouse> wareHouse = InventoryInfoMgt.GetWarehouses();
            return (from house in wareHouse
                    where includeAll || !house.IsCentral
                    select new SelectListItem()
                    {
                        Text = house.Name,
                        Value = house.Id.ToString(),
                        Selected = house.Id.ToString() == id 
                    }).ToList();
        }

        public static string GetDOStatus(int status)
        {
            switch (status)
            {
                case 0: return "Pending";
                case 1: return "Voied";
                case 2: return "Processing";
                case 3: return "Delived";
            }
            return string.Empty;
        }

        public static int GetStatusCount(List<ItemCountModel> model, int status)
        {
            var sb = model.Find(n => n.Status == status);
            if (sb == null) return 0;
            return sb.Number;
        }

        public static int GetRequisitionNumber(Product product, List<RequisitionDetail> details, string masterId)
        {
            if (details == null || details.Count == 0) return 0;
            RequisitionDetail detail = details.Find(n => n.ProductId == product.Id && n.ResuisitionId.ToString() == masterId);
            if (detail == null) return 0;
            return (int)detail.RequestedQty;
        }

        public static Product GetProductById(Guid id)
        {
            return InventoryInfoMgt.GetProductById(id);
        }

        public static string GetRequisitionStatus(int status)
        {
            switch (status)
            {
                case 0: return "Processing";
                case 1: return "Pending";
                case 2: return "Approved";
                case 3: return "Reject";
                case 4: return "Cancelled";
                case 5: return "Delived";
            }
            return string.Empty;
        }

        public static string GetGRNStatus(int status)
        {
            switch (status)
            {
                case 0: return "Pending";
                case 1: return "Posted";
                case 2: return "Cacelled";
            }
            return string.Empty;
        }
    }
}